/*
 * *
 *  * Created with IntelliJ RustRover.
 *  * Description:
 *  * Copyright Author: ZhenYi
 *  * DateTime: 2024/2/15 下午10:35
 *
 */

use std::sync::{Arc, Mutex};
use rustis::client::Client;
use rustis::commands::ConnectionCommands;
use rustis::resp::cmd;

#[allow(unused)]
pub enum BasicAuthOps{
    AddBasic,
    DelBasic,
    UpdateBasic
}
#[allow(unused)]
pub struct BasicAuthQueue{
    ops: BasicAuthOps,
    cookie:(String,String)
}
impl BasicAuthQueue {
    #[allow(unused)]
    pub fn new(ops:BasicAuthOps,cookie:(String,String)) -> BasicAuthQueue {
        BasicAuthQueue {
            ops,
            cookie
        }
    }
    #[allow(unused)]
    pub async fn add_basic(&self,client:Client) {
        client.select(5).await.unwrap();
        client.send(cmd("SET").arg(self.cookie.0.clone()).arg(self.cookie.1.clone()),None).await.unwrap();
        client.send(cmd("EXPIRE").arg(self.cookie.0.clone()).arg("3600"),None).await.unwrap();
    }
    #[allow(unused)]
    pub async fn del_basic(&self,client:Client) {
        client.select(5).await.unwrap();
        client.send(cmd("DEL").arg(self.cookie.0.clone()),None).await.unwrap();
    }
    #[allow(unused)]
    pub async fn update_basic(&self,client:Client) {
        client.select(5).await.unwrap();
        client.send(cmd("EXPIRE").arg(self.cookie.0.clone()).arg("3600"),None).await.unwrap();
    }
    #[allow(unused)]
    pub async fn match_ops(&self,client: Arc<Mutex<Client>>) {
        let client = client.lock().unwrap().clone();
        client.select(0).await.unwrap();
        match self.ops {
            BasicAuthOps::AddBasic => self.add_basic(client).await,
            BasicAuthOps::DelBasic => self.del_basic(client).await,
            BasicAuthOps::UpdateBasic => self.update_basic(client).await
        }
    }
}